package com.example.tests;

import static org.testng.Assert.*;
import static org.hamcrest.MatcherAssert.*;
import static org.hamcrest.Matchers.*;

import org.junit.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import com.example.fw.AccountHelper;
import com.example.fw.JamesHelper;
import com.example.fw.User;


public class SignupTest extends TestBase{
	
	
	public User user = new User().setLogin("testUser1")
			.setEmail("testUser1@localhost.localdomain").setPassword("123456");
	
		
	private JamesHelper james;

	private AccountHelper accHelper;

	@BeforeClass
	public void createMailUser() {
		james = app.getJamesHelper();
		accHelper = app.getAccountHelper();
		
		if (! james.doesUserExist(user.login)) {
			james.createUser(user.login, user.password);
		}
	}
	
	@Test
	public void aNewUserShouldSignup() {
		accHelper.signup(user);
		accHelper.login(user);
		assertThat(app.getAccountHelper().loggedUser(), equalTo(user.login));
		}
	
	@Test
	public void existingUserShouldNotSignup() {
		try {
		accHelper.signup(user);
		} catch (Exception e) {
			assertThat(e.getMessage(), containsString("That username is already being used. Please go back and select another one."));
			return;
		}
		fail("Exception expected");
		}
	
	
	
	@AfterClass
	public void deleteMailUser() {
		if (james.doesUserExist(user.login)) {
			james.deleteUser(user.login);
		}
	}
	

}
